﻿using System;
using Machine.Entity;
using System.Linq.Expressions;
using Machine.Model.ReturnModel;
using Machine.IRepository;
using Machine.Repository;
using Machine.Helper;
using System.IO;

namespace Machine.Bussiness
{
    public class AttachmentListBLL : BaseBLL<AttachmentList>
    {
        private IAttachmentList DAL = new AttachmentListDAL();

        public override void SetCurrentRepository()
        {
            CurrentRepository = DAL;//CurrentRepository属性在BaseBLL中定义
        }

        public void SaveFile(string guid, string filePath, string fileName, string uid)
        {
            //保存图片
            FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
            string ext = "jpg";
            long size = fs.Length;
            byte[] imgByte = new byte[size];
            fs.Read(imgByte, 0, (int)size);
            fs.Close();
            fs = null;

            //新增记录
            AttachmentList Entity = new AttachmentList();
            Entity.guid = guid;
            Entity.FileName = fileName;
            Entity.Creater = uid;
            Entity.Updater = uid;
            Entity.Createtime = DateTime.Now;
            Entity.Updatetime = DateTime.Now;
            Entity.FileExt = ext;
            Entity.FileSize = size;
            Entity.Remark = "";
            Entity.启用 = true;
            int id = DAL.InsertReturnIdentity(Entity);

            AttAttachmentListDAL AttDAL = new AttAttachmentListDAL();
            AttAttachmentList AttEntity = new AttAttachmentList();
            AttEntity.ID = id;
            AttEntity.FileBody = imgByte;
            AttEntity.Createtime = DateTime.Now;
            AttDAL.Add(AttEntity);
        }
    }
}
